<?php
namespace Home\Controller;
use Think\Controller;
class DepartmentController extends CommonController {

    protected $departmentModel;
    protected $store;
    public function _initialize(){
        parent::_initialize();
        $this->departmentModel = D("store_department");
        $this->store = D("store");
    }

    //部门列表
    public function index(){
        $where = array();
        $page = I('p') ? I('p') : '1';
        $pageSize = I('pageSize') ? I('pageSize') : 15;
        $where['store_id'] = $this->sessionInfo['user_id'];
        $count = $this->departmentModel->where($where)->count();
        $list = $this->departmentModel->where($where)->page($page,$pageSize)->order('department_id desc')->select();
        $show = $this->getPage($count,$page,$pageSize);
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->display();
    }

    //添加部门
    public function add(){
        if(IS_POST){
            $post = $_POST;
            $this->validate('部门名称',$post['department_name'],'',1);
            $this->validate('店铺',$post['store_id'],'',5,array(0,999999999));
            $addData['department_name'] = $post['department_name'];
            $addData['store_id'] = $_POST['store_id'];
            $info = $this->departmentModel->add($addData);
            if($info){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            //判断登录账号是否为总店铺  1是 0不是
            if($this->sessionInfo['parent_id'] == 0){
                $map['store_id']  = $this->sessionInfo['user_id'];
                $map['parent_id']  = $this->sessionInfo['user_id'];
                $map['_logic'] = 'or';
                $where['_complex'] = $map;
            }else{
                $where['store_id'] = $this->sessionInfo['user_id'];
            }
            $list = $this->store->where($where)->select();
            $this->assign('list',$list);
            $this->display();
        }
    }

    //编辑部门
    public function edit(){
        if(IS_POST){
            $post = $_POST;
            $department_id = $_POST['department_id'];
            if(!$department_id){
                $this->error('信息传输错误');
            }
            $this->validate('部门名称',$post['department_name'],'',1);
            $this->validate('店铺',$post['store_id'],'',5,array(0,999999999));
            $addData['department_name'] = $post['department_name'];
            $addData['store_id'] = $_POST['store_id'];
            $info = $this->departmentModel->where(array('department_id'=>$department_id))->save($addData);
            $save = M("store_relationship")->where(array('department_id'=>$department_id))->save(array('department_name'=>$post['department_name']));
            $this->success('保存成功');
        }else{
            $this->validate('数据错误','department_id','',1);
            $info = $this->departmentModel->where(array('department_id'=>$_GET['department_id']))->find();
            if(empty($info)){
                $this->error('该部门不存在');
            }
            //判断登录账号是否为总店铺  1是 0不是
            if($this->sessionInfo['parent_id'] == 0){
                $map['store_id']  = $this->sessionInfo['user_id'];
                $map['parent_id']  = $this->sessionInfo['user_id'];
                $map['_logic'] = 'or';
                $where['_complex'] = $map;
            }else{
                $where['store_id'] = $this->sessionInfo['user_id'];
            }
            $list = $this->store->where($where)->select();
            $this->assign('info',$info);
            $this->assign('list',$list);
            $this->display();
        }
    }

    //删除部门
    public function delete(){
        $department_id = $_GET['department_id'];
        if(!$department_id){
            $this->error('信息传输错误');
        }
        $info = $this->departmentModel->where(array('department_id'=>$department_id))->delete();
        if($info){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }

    }

    //导出
    public function excel(){
        $department_id = $_GET['department_id'];
        if(!$department_id){
            $this->error('信息传输错误');
        }
        $info = $this->departmentModel->where(array('department_id'=>$department_id))->find();
        //查出所有的员工
        $staffList = D('store_relationship')->where(array("department_id"=>$department_id))->select();
        if(empty($staffList)){
            $this->error('所选数据为空！');
        }
        $ids = '';
        foreach($staffList as $k=>$v){
            if($k == 0){
                $ids = $v['son_id'];
            }else{
                $ids .= ','.$v['son_id'];
            }
        }
        $where['fourth_class'] = array("in",$ids);
        $member = D("member");
        $list = $member->where($where)->order('reg_time desc')->select();
        if(!empty($list)){
            foreach($list as $k=>$v){
                $list[$k]['third_name'] = $member->where(array('user_id'=>$v['third_class']))->getField("real_name");
                $list[$k]['second_name'] = $member->where(array('user_id'=>$v['second_class']))->getField("user_name");
            }

            require '../sellerserver/ThinkPHP/Library/Vendor/PHPExcel/PHPExcel.php';
            $excel = new \PHPExcel();

            $objActSheet = $excel->getActiveSheet();
            $objActSheet->setCellValue('A1', '用户名');
            $objActSheet->setCellValue('B1', '手机号');
            $objActSheet->setCellValue('C1', '推广人');
            $objActSheet->setCellValue('D1', '所属店铺');
            $objActSheet->setCellValue('E1', '注册时间');
            $objActSheet->setCellValue('F1', '所属部门');

            foreach ($list as $k => $v){
                $k +=2;
                $time = date('Y-m-d H:i:s',$v['reg_time']);
                $objActSheet->setCellValue('A'.$k, $v['user_name']);
                $objActSheet->setCellValue('B'.$k, $v['phone_mob']);
                $objActSheet->setCellValue('C'.$k, $v['third_name']);
                $objActSheet->setCellValue('D'.$k, $v['second_name']);
                $objActSheet->setCellValue('E'.$k, $time);
                $objActSheet->setCellValue('F'.$k, $info['department_name']);
            }
            $excel = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');  //Excel2007  兼容好
            ob_end_clean();   //解决乱码
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename=部门员工推广详细' . date('Ymd') . '.xlsx');
            header('Cache-Control: max-age=0');
            $excel->save('php://output');
            exit;
        }else{
            $this->error('您所选的数据为空！');
        }


    }
}